#include "FilesManager.h"
#include <iomanip>
#include <sstream>
#include <fstream>
#include "Global.h"
#include <algorithm>
#include <cmath>

FilesManager::FilesManager()
{

}

void FilesManager::SetDir(const std::string& xdir)
{
    GLOB.PROJECT_DIR = xdir;
}

//FilesManager::FilesManager(const FilesManager& orig)
//{
//}

FilesManager::~FilesManager()
{
}

/*     podprogram zrzucajacy dane do rysowania programem surfer */

int FilesManager::SaveMSH000(const std::string& typ, double *x, int nlv, int nlh)
{

    int i1, j1, k1;

    FL7.open((GLOB.PROJECT_DIR + typ + "_msh.000").c_str(), std::ios_base::binary | std::ios_base::out); // ts_msh.000

    // nieuzywane !!!!!!!!
    //double alf5 = atan(1.) / 180.;
    //double nwp = nlv * nlh;

    scale(GLOB.mnmx.sc, GLOB.mnmx.sc);
    setpen(1);

    /* ----> siec: */

    for (int i = 0; i < nlh; ++i)
    {
        i1 = 2 * (i + 1);
        moveto(GLOB.rnorm * x[i1 - 2], GLOB.rnorm * x[i1 - 1]);
        for (int k = 1; k <= nlv - 1; ++k)
        {
            k1 = 2 * (i + 1 + k * nlh);
            drawto(GLOB.rnorm * x[k1 - 2], GLOB.rnorm * x[k1 - 1]);
        }
    }

    for (int i = 0; i < nlv; ++i)
    {
        i1 = 2 * (i * nlh + 1);
        moveto(GLOB.rnorm * x[i1 - 2], GLOB.rnorm * x[i1 - 1]);
        for (int j = 2; j <= nlh; ++j)
        {
            j1 = 2 * (i * nlh + j);
            drawto(GLOB.rnorm * x[j1 - 2], GLOB.rnorm * x[j1 - 1]);
        }
    }

    FL7.close();

    return 0;
}

int scale(double xsc, double ysc)
{

    FM.FL7 << "SC " << std::fixed << std::setw(20) << std::setprecision(10) << xsc
            << std::setw(20) << std::setprecision(10) << ysc << "\n";

    return 0;
}

int setpen(int ipen)
{

    FM.FL7 << "SP " << std::fixed << std::setw(10) << ipen << "\n";
    return 0;
}

int drawto(double xsc, double ysc)
{

    FM.FL7 << "PA " << std::fixed << std::setw(20) << std::setprecision(10) << -GLOB.mnmx.xmin + xsc
            << std::setw(20) << std::setprecision(10) << -GLOB.mnmx.ymin + ysc << "\n";

    return 0;
}

int moveto(double xsc, double ysc)
{

    FM.FL7 << "MA " << std::fixed << std::setw(20) << std::setprecision(10) << -GLOB.mnmx.xmin + xsc
            << std::setw(20) << std::setprecision(10) << -GLOB.mnmx.ymin + ysc << "\n";

    return 0;
}

int FilesManager::piszbrg(int mw, int nw, double *xw, double rnorm)
{

    int iw = mw * nw;

    for (int i = 1; nw < 0 ? i >= iw : i <= iw; i += nw)
    {
        FL4 << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xw[2 * i - 2]
                << std::setw(10) << std::setprecision(2) << rnorm * xw[i * 2 - 1] << "\n";
    }

    for (int i = iw - nw + 2; i <= iw; ++i)
    {
        FL4 << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xw[2 * i - 2]
                << std::setw(10) << std::setprecision(2) << rnorm * xw[i * 2 - 1] << "\n";
    }

    for (int i = iw - nw; nw > 0 ? i >= nw : i <= nw; i -= nw)
    {
        FL4 << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xw[2 * i - 2]
                << std::setw(10) << std::setprecision(2) << rnorm * xw[2 * i - 1] << "\n";
    }

    for (int i = nw - 1; i >= 1; --i)
    {
        FL4 << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xw[2 * i - 2]
                << std::setw(10) << std::setprecision(2) << rnorm * xw[2 * i - 1] << "\n";
    }

    return 0;
}

int FilesManager::piszbln(int mw, int nw, double *xw, double rnorm)
{

    int ix, iy;

    int iw = mw * nw;
    double xmin = 1.;
    double ymin = 1.;
    double xmax = -1.;
    double ymax = -1.;

    for (int i = 0; i < iw; ++i)
    {
        iy = 2 * (i + 1);
        ix = iy - 1;
        xmin = std::min(xmin, xw[ix - 1]);
        ymin = std::min(ymin, xw[iy - 1]);
        xmax = std::max(xmax, xw[ix - 1]);
        ymax = std::max(ymax, xw[iy - 1]);
    }
    xmin *= 1.001;
    ymin *= 1.001;
    xmax *= 1.001;
    ymax *= 1.001;

    FL4 << " " << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xmin
            << std::setw(10) << std::setprecision(2) << rnorm * xw[1] << "\n";

    piszbrg(mw, nw, xw, rnorm);

    FL4 << " " << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xmin
            << std::setw(10) << std::setprecision(2) << rnorm * xw[1] << "\n";
    FL4 << " " << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xmin
            << std::setw(10) << std::setprecision(2) << rnorm * ymin << "\n";
    FL4 << " " << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xmax
            << std::setw(10) << std::setprecision(2) << rnorm * ymin << "\n";
    FL4 << " " << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xmax
            << std::setw(10) << std::setprecision(2) << rnorm * ymax << "\n";
    FL4 << " " << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xmin
            << std::setw(10) << std::setprecision(2) << rnorm * ymax << "\n";
    FL4 << " " << std::fixed << std::setw(10) << std::setprecision(2) << rnorm * xmin
            << std::setw(10) << std::setprecision(2) << rnorm * xw[1] << "\n";

    return 0;
}

int FilesManager::SaveBLNs(const std::string& typ)
{

    std::string nfile;

    //  ---------------------   obsluga zbiorow BLN    -------------------------

    nfile = typ + "_b.bln";
    FL4.open((GLOB.PROJECT_DIR + nfile).c_str(), std::ios_base::binary | std::ios_base::out);
    FL4 << std::setw(10) << 2 * (GLOB.nkp.mw + GLOB.nkp.nw) - 3 << std::setw(10) << std::string("0") << "\n";
    piszbrg(GLOB.nkp.mw, GLOB.nkp.nw, GLOB.zargd.xw, GLOB.rnorm);
    FL4.close();

    nfile = typ + ".bln";
    FL4.open((GLOB.PROJECT_DIR + nfile).c_str(), std::ios_base::binary | std::ios_base::out);
    FL4 << std::setw(10) << 2 * (GLOB.nkp.mw + GLOB.nkp.nw) + 4 << std::setw(10) << std::string("1") << "\n";
    piszbln(GLOB.nkp.mw, GLOB.nkp.nw, GLOB.zargd.xw, GLOB.rnorm);
    FL4.close();

    return 0;
}

int FilesManager::SaveDATs(const std::string& typ, int ichl, int iw, std::vector<std::vector<double> >& v_tf)
{
    std::string nfile, nrchl;
    std::ofstream FL3;

    for (int ich = 1; ich <= ichl; ++ich) // ilosc chlodzen
    {
        std::ostringstream ss;
        ss << std::setfill('0') << std::setw(3) << ich;
        nrchl = ss.str();

        nfile = typ + nrchl + ".dat";
        FL3.open((GLOB.PROJECT_DIR + nfile).c_str(), std::ios_base::binary | std::ios_base::out); // ts001.dat - ts007.dat
        FL3 << std::fixed << std::setw(10) << iw << "\n";
        for (int i = 0; i < iw; ++i)
        {
            FL3 << std::setprecision(2) << std::setw(10) << GLOB.rnorm * GLOB.zargd.xw[2 * i] <<
                    std::setw(10) << GLOB.rnorm * GLOB.zargd.xw[2 * i + 1] << std::setw(10) << v_tf[ich - 1][i] << "\n";
        }
        FL3.close();
    }
    return 0;
}

int FilesManager::SaveTEMLOGs(int ichl, const std::vector<int>& v_idt, const std::vector<std::vector<std::vector<double> > >& v_tpc)
{
    std::ofstream FL11;
    std::string temnr, temstr;

    for (int ich = 1; ich <= ichl; ++ich) // ilosc chlodzen
    {
        // std::cout << "\nChlodzenie: " << ich << "/" << ichl;

        //    temper(ral___.ispray, ich, nkp_.mw, nkp_.nw, v_ndt[ich - 1], v_dt[ich - 1],
        //         iprint, &idt, istem, temgrd, &dtemgrd, iwn, tempgrd, &dtempgrd);

        if (ich > 1)
        {
            std::ostringstream ss;
            ss << std::setfill('0') << std::setw(3) << ich - 1;
            temnr = ss.str();

            temstr.assign("temlog.");
            temstr.append(temnr);

            FL11.open((GLOB.PROJECT_DIR + temstr).c_str(), std::ios_base::binary | std::ios_base::out); // temlog.001 - temlog.006
            FL11.setf(std::ios::fixed);
            FL11 << std::setw(10) << std::setprecision(1) << 0.e0;

            for (int j = 1; j < v_idt[ich - 1] + 2; ++j)
            {
                FL11 << std::setw(10) << std::setprecision(1) << v_tpc[ich - 1][j][0];
            }
            FL11 << "\n";

            for (int i = 1; i < GLOB.nkp.mw + 1; ++i)
            {
                for (int j = 0; j < v_idt[ich - 1] + 2; ++j)
                {
                    FL11 << std::setw(10) << std::setprecision(1) << v_tpc[ich - 1][j][i];
                }
                FL11 << "\n";
            }
            FL11.close();
        }
    }
    return 0;
}

int FilesManager::SaveOUT(int iwkod, int iw, int ie, int ichl, int mw, int nw,
                          std::vector<int>& v_ndt, std::vector<double>& v_tchcal,
                          std::vector<std::vector<double> >& v_t, std::vector<bool>& v_show_t)
{
    int i1, i2;
    std::vector<double>::iterator it_tchcal = v_tchcal.begin();
    std::vector<std::vector<double> >::iterator it_t = v_t.begin();
    std::vector<bool>::iterator it_show_t = v_show_t.begin();


    // wypisywanie podstawowych danych dot. struktury elementow (o ile ustawiono opcje w .inp)
    if (iwkod == 1)
    {
        FL2 << "\n\n\n   el.   wezel" <<
                std::setw(6) << "1" << std::setw(6) << "2" << std::setw(6) << "3" << std::setw(6) << "4" <<
                std::setw(17) << "kod-t\n\n";

        for (int i = 0; i < ie; ++i)
        {
            FL2 << std::setw(6) << i + 1 << std::setw(8) << " ";
            for (int j = 0; j < 4; ++j)
            {
                FL2 << std::setw(6) << GLOB.iy[i][j];
            }
            FL2 << std::setw(14) << GLOB.kod[i] << "\n";
        }

        FL2 << "\n\n" << std::setw(10) << " " << "wspolrzedne wezlow\n\n" << std::setw(12) << " " <<
                "wezel" << std::setw(22) << " " << "x" << std::setw(17) << " " << "y" <<
                std::setw(13) << " " << "kod-e\n\n";

        for (int i = 0; i < iw; ++i)
        {
            FL2 << std::setw(15) << i + 1 << std::setw(10) << " " << std::setw(18) <<
                    std::setprecision(8) << GLOB.zargd.xw[2 * i] << std::setw(18) <<
                    GLOB.zargd.xw[2 * i + 1] << std::setw(14) << GLOB.ib[i] << "\n";
        }
    }

    for (int l = 1; l <= ichl; l++)
    {
        FL2 << "\n Chlodzenie nr.       " << l << "\n\n";
        FL2 << "\n Chlodzenie nr.       " << l << "\n\n";

        FL2 << "\n\n\n\n\n czas chlodzenia" << std::fixed << std::setw(10) << std::setprecision(3) << *it_tchcal << "   s\n";
        FL2 << "\n\n\n\n\n czas chlodzenia" << std::fixed << std::setw(10) << std::setprecision(3) << *it_tchcal << "   s\n";

        FL2 << "\n\n pole temperatur                                                  -----> gora\n\n\n";
        FL2 << "\n\n pole temperatur                                                  -----> gora\n\n\n";

        for (int j = 0; j < mw; ++j)
        {
            i1 = j * nw + 1;
            i2 = (j + 1) * nw;

            FL2 << " ";
            for (int i = i1; i <= i2; ++i)
            {
                FL2 << std::setw(4) << static_cast<int> ((*it_t)[i - 1]);
            }
            FL2 << "\n";

            FL2 << " ";
            for (int i = i1; i <= i2; ++i)
            {
                FL2 << std::setw(4) << static_cast<int> ((*it_t)[i - 1]);
            }
            FL2 << "\n";
        }

        *it_tchcal++;
        *it_t++;

        for (int ik = 0; ik < v_ndt[l - 1]; ++ik)
        {
            if (*it_show_t++)
            {
                FL2 << "\n\n\n\n\n czas chlodzenia" << std::fixed << std::setw(10) << std::setprecision(3) << *it_tchcal << "   s\n";
                FL2 << "\n\n\n\n\n czas chlodzenia" << std::fixed << std::setw(10) << std::setprecision(3) << *it_tchcal << "   s\n";

                FL2 << "\n\n pole temperatur                                                  -----> gora\n\n\n";
                FL2 << "\n\n pole temperatur                                                  -----> gora\n\n\n";

                for (int j = 0; j < mw; ++j)
                {
                    i1 = j * nw + 1;
                    i2 = (j + 1) * nw;

                    FL2 << " ";
                    for (int i = i1; i <= i2; ++i)
                    {
                        FL2 << std::setw(4) << static_cast<int> ((*it_t)[i - 1]);
                    }
                    FL2 << "\n";

                    FL2 << " ";
                    for (int i = i1; i <= i2; ++i)
                    {
                        FL2 << std::setw(4) << static_cast<int> ((*it_t)[i - 1]);
                    }
                    FL2 << "\n";
                }

                *it_tchcal++;
                *it_t++;

            }
        }

        FL2 << "\n\n";
    }

    FL2.close();

    return 0;
}



/* ----> podprogram ma za zadanie odnalezienie linii z danymi ( zaczynajacej */
/* ----> sie od znaku ".") oraz ustawienie pozycji czytania na polu */
/* ----> danych (68 kolumna) */

// z pliku ts.inp

int FilesManager::skipinp(std::string &dana)
{

    std::string line;

    do
    {
        std::getline(FL1, line, '\n');
    }
    while (line[0] != '.');

    if ((line.size() > 0) && (line[line.size() - 1] == '\r'))
        line.resize(line.size() - 1);

    // usun spacje z konca, jesli trzeba
    line.erase(std::remove_if(line.end() - 1, line.end(), isspace), line.end());
    //std::cout << line << std::endl;
    FL17 << line << "\n";

    dana.assign(line, 67, 12);
    dana.erase(std::remove_if(dana.begin(), dana.end(), isspace), dana.end());

    return 0;
}

int FilesManager::SavePLG(const std::string& typ, const std::vector<std::vector<double> >& vv_plg)
{
    std::string typsld = typ + ".plg"; // uzywane tylko w funkcji 'temper -> putemp'
    FL19.open((GLOB.PROJECT_DIR + typsld).c_str(), std::ios_base::binary | std::ios_base::out);

    for (unsigned int i = 0; i < vv_plg.size(); ++i)
    {
        FL19 << std::setw(8) << std::fixed << std::setprecision(3) << vv_plg[i][0]; // pierwsza kolumna pliku .plg           
        for (unsigned int j = 0; j < vv_plg[i].size() - 1; ++j)
        {
            FL19 << std::setw(7) << std::fixed << std::setprecision(1) << vv_plg[i][j + 1]; // kolejne kolumny pliku .plg 
        }
        FL19 << "\n";
    }
    FL19.close();

    return 0;
}

int FilesManager::SaveTLG(const std::string& typ, int xistem, const std::vector<std::vector<double> >& vv_tlg)
{
    std::string typsld = typ + ".tlg";
    FL18.open((GLOB.PROJECT_DIR + typsld).c_str(), std::ios_base::binary | std::ios_base::out);
    FL18 << std::setw(6) << " ";
    for (int i = 0; i < xistem; ++i)
    {
        FL18 << std::setw(7) << GLOB.nstem[i]; // zapisanie wczytanych wczesniej indeksow
    }
    FL18 << "\n  czas\n\n";

    for (unsigned int i = 0; i < vv_tlg.size(); ++i)
    {
        FL18 << std::setw(8) << std::fixed << std::setprecision(3) << vv_tlg[i][0]; // pierwsza kolumna pliku .plg           
        for (unsigned int j = 0; j < vv_tlg[i].size() - 1; ++j)
        {
            FL18 << std::setw(7) << std::fixed << std::setprecision(1) << vv_tlg[i][j + 1]; // kolejne kolumny pliku .plg 
        }
        FL18 << "\n";
    }

    FL18.close();
    return 0;
}

void FilesManager::LoadINP(const std::string& typ)
{
    std::string data;

    GLOB.rnorm = 1000.;

    std::string typint = typ + ".inp";
    FL1.open((GLOB.PROJECT_DIR + typint).c_str()); // .inp
    if (!FL1.is_open())
    {
        std::cout << "Blad otwarcia pliku " << typint << std::endl;
        exit(1);
    }

    std::string typdlg = typ + ".dlg";
    FL17.open((GLOB.PROJECT_DIR + typdlg).c_str(), std::ios_base::binary | std::ios_base::out); // ts.dlg

    /*     ------------------------------------------------------------------ */
    /*     np     - ilosc segmentow zarysu brzegu pasma */
    /*     Nkszt  - numer wykroju podlegajacej chlodzeniu */
    /*            1 - szyna S60, ksztalt gotowy */
    /*     ------------------------------------------------------------------ */

    GLOB.nkp.nkszt = FromString<int>(data);

    if (GLOB.nkp.nkszt > NKSZTMAX)
    {
        std::cout << "Maksymalny numer pasma: " << NKSZTMAX << std::endl;
        FL1.close();
        exit(1);
    }

    /* ----> LINIE PODZIALU */
    GLOB.nkp.mw = FromString<int>(data);
    if (GLOB.nkp.mw > 0)
    {
        GLOB.mPodzDefault = false;
        for (int i = 0; i < GLOB.nkp.mw - 1; ++i)
        {
            GLOB.zargd.xpodz[i + 1] = FromString<double>(data);
        }
    }
    else
    {
        GLOB.mPodzDefault = true;
        GLOB.nkp.mw = GLOB.maczar.mwprep[GLOB.nkp.nkszt - 1];
        for (int i = 0; i < GLOB.nkp.mw - 1; ++i)
        {
            GLOB.zargd.xpodz[i + 1] = GLOB.maczar.xprep[GLOB.nkp.nkszt - 1][i];
        }
    }

    GLOB.nkp.nw = FromString<int>(data);
    if (GLOB.nkp.nw > 0)
    {
        GLOB.nPodzDefault = false;
        for (int i = 0; i < GLOB.nkp.nw - 1; ++i)
        {
            GLOB.zargd.ypodz[i + 1] = FromString<double>(data);
        }
    }
    else
    {
        GLOB.nPodzDefault = true;
        GLOB.nkp.nw = GLOB.maczar.nwprep[GLOB.nkp.nkszt - 1];
        for (int i = 0; i < GLOB.nkp.nw - 1; ++i)
        {
            GLOB.zargd.ypodz[i + 1] = GLOB.maczar.yprep[GLOB.nkp.nkszt - 1][i];
        }
    }

    GLOB.nkp.me = GLOB.nkp.mw - 1;
    GLOB.nkp.ne = GLOB.nkp.nw - 1;
    GLOB.iw = GLOB.nkp.nw * GLOB.nkp.mw;
    GLOB.ie = GLOB.nkp.me * GLOB.nkp.ne;


    // -----  wczytanie indeksow wezlow, w ktorych ma byc sledzona temp.  -------

    GLOB.istem = FromString<int>(data);
    if (GLOB.istem < 1) //   ilosc wezlow, w ktorych ma byc sledzona temp.
    {
        GLOB.istem = 1;
        GLOB.nstem[0] = 0; // indeks wezla
        GLOB.temgrd = 1.;
    }
    else
    {
        for (int i = 0; i < GLOB.istem; ++i)
        {
            GLOB.nstem[i] = FromString<int>(data);

            if (GLOB.nstem[i] < 1)
            {
                GLOB.nstem[i] = 1;
            }
            if (GLOB.nstem[i] > GLOB.iw)
            {
                GLOB.nstem[i] = GLOB.iw;
            }
        }

        // minimalny przyrost temperatury, po ktorym nastepuje wydruk (1)
        GLOB.temgrd = FromString<double>(data);
    }

    // --  wczytanie wspolrzednych dod. punktow, w ktorych ma byc sledzona temp.

    GLOB.iwn = FromString<int>(data); // ilosc dod. punktow, w ktorych ma byc sledzona temp.
    if (GLOB.iwn > 0)
    {
        for (int i = 0; i < GLOB.iwn; ++i)
        {
            GLOB.xn[i] = FromString<double>(data);
            GLOB.yn[i] = FromString<double>(data);

            GLOB.xn[i] /= GLOB.rnorm;
            GLOB.yn[i] /= GLOB.rnorm;

            GLOB.valxn[i] = 0.;
            GLOB.valyn[i] = 0.;
        }

        // minimalny przyrost temperatury, po ktorym nastepuje wydruk (2)
        GLOB.tempgrd = FromString<double>(data);
    }

    /*     temperatura */

    // wypisywanie podstawowych danych dot. struktury elementow ?
    GLOB.iwkod = FromString<int>(data);

    // co ile krokow wydruk temperatury
    GLOB.iprint = FromString<int>(data);

    // temp. powietrza
    GLOB.ral.tpow = FromString<double>(data);

    // temp. pasma
    GLOB.tpas0 = FromString<double>(data);

    // chlodzenie lewej strony pasma ?
    GLOB.kkod1 = FromString<int>(data);

    // chlodzenie prawej strony pasma ?
    GLOB.kkod2 = FromString<int>(data);

    // chlodzenie dolnej strony pasma ?
    GLOB.kkod3 = FromString<int>(data);

    // chlodzenie gornej strony pasma ?
    GLOB.kkod4 = FromString<int>(data);

    // czas biezacy w chwili rozpoczecia chlodzenia
    GLOB.ral.tchcal = FromString<double>(data);

    // ilosc chlodzen
    GLOB.ichl = FromString<int>(data);

    GLOB.ral.ralfa.resize(GLOB.ichl);
    GLOB.ral.ralfpo.resize(GLOB.ichl);
    GLOB.ral.ralfpn.resize(GLOB.ichl);
    GLOB.v_ndt.resize(GLOB.ichl);
    GLOB.v_dt.resize(GLOB.ichl);

    for (int ich = 0; ich < GLOB.ichl; ++ich) // ilosc chlodzen
    {
        // ilosc krokow czasowych kolejnego cyklu chlodzenia
        GLOB.v_ndt[ich] = FromString<int>(data);

        // krok czasowy chlodzenia
        GLOB.v_dt[ich] = FromString<double>(data);

        // wspolczynnik przejmowania ciepla
        GLOB.ral.ralfa[ich] = FromString<double>(data);
        GLOB.ral.ralfpo[ich] = GLOB.ral.ralfa[ich];
        GLOB.ral.ralfpn[ich] = GLOB.ral.ralfa[ich];
        GLOB.ral.ispray = false;
    }


    // dodane ze start
    int j1;
    for (int j = 0; j < GLOB.nkp.mw; ++j)
    {
        j1 = j * GLOB.nkp.nw;

        for (int k = 0; k < GLOB.nkp.nw; ++k)
        {
            int i = j1 + k;
            if (j == 0 || j == GLOB.nkp.mw - 1 || k == 0 || k == GLOB.nkp.nw - 1)
            {
                GLOB.ib[i] = 10;
            }
            else
            {
                GLOB.ib[i] = 0;
            }
            GLOB.tp[i] = GLOB.tpas0;
            GLOB.tprzem[i] = -1.;
            GLOB.q[i] = 0.;
            GLOB.xvv[i] = 0.;
            GLOB.dteps[i] = 0.;
            GLOB.tweps[i] = 0.;
        }
    }

    podz(GLOB.nkp.me, GLOB.nkp.ne, GLOB.nkp.nw); // podzial na elementy
    wycin(GLOB.ie, GLOB.nkp.ne, GLOB.kkod1, GLOB.kkod2, GLOB.kkod3, GLOB.kkod4);

    // DONE temhist przeniesione do temp


    // koniec

    FL1.close();

    FL17.close();

}



/* ----> podprogram podz przyporzadkowuje poszczegolnym wezlom elementu */
/* ----> numery wezlow wlasciwe dla calej struktury. */
/* ----> znaczenie parametrow: */
/*     ----> ne  - ilosc elementow w kierunku y */
/*     ----> nw  - ilosc wezlow w kierunku y */
/*     ----< nel - calkowita ilosc elementow */
/*     ----> y   - tablica przyporzadkowujaca numerom wezlow w elemencie */
/*     ---->       numery wezlow calej struktury */
/*      ----> y(4*(n-1)+1),...,y(4*(n-1)+4) - kolejne wezly n-tego */

/*      ---->                         elementu */

int FilesManager::podz(int me, int ne, int nw)
{

    int k1, nel = 0;

    for (int i = 0; i < me; ++i)
    {
        k1 = i * nw;
        for (int j = 0; j < ne; ++j)
        {
            GLOB.iy[nel][0] = k1 + j + 1;
            GLOB.iy[nel][1] = GLOB.iy[nel][0] + nw;
            GLOB.iy[nel][2] = GLOB.iy[nel][1] + 1;
            GLOB.iy[nel][3] = GLOB.iy[nel][0] + 1;
            ++nel;
        }
    }
    return 0;
}


/* ----> podprogram kodujacy mozliwe do chlodzenia boki elementow */
/* ----> oraz wypisujacy je wraz z kodami styku metalu i narzedzia */
/* ----> i przyporzadkowaniem wezlow globalnych elementom. */

// uzupelnia plik .out informacjami o wspolrzednych wezlow i elementach

int FilesManager::wycin(int ie, int ne, int kkod1, int kkod2, int kkod3, int kkod4)
{

    for (int i = 0; i < ie; ++i)
    {
        GLOB.kod[i] = 0;
    }

    for (int i = 0; i < ne; ++i)
    {
        GLOB.kod[i] = kkod1;
    }

    for (int i = ie - ne + 1; i <= ie; ++i)
    {
        GLOB.kod[i - 1] = GLOB.kod[i - 1] * 10 + kkod2;
    }

    for (int i = 1; ne < 0 ? i >= ie : i <= ie; i += ne)
    {
        GLOB.kod[i - 1] = GLOB.kod[i - 1] * 10 + kkod3;
    }

    for (int i = ne; ne < 0 ? i >= ie : i <= ie; i += ne)
    {
        GLOB.kod[i - 1] = GLOB.kod[i - 1] * 10 + kkod4;
    }

    return 0;
}

/* ----> ustawienie katow i odleglosci w linii segmentowej */

int FilesManager::sett(const std::string& finame, double sc, int n,
                       double *xz, double *yz, double *rz, double *xrz,
                       double *yrz, double *alp, double *alk, double *t)
{

    int num = 30;
    double al, dl, tb, dt, pi, dx, dy, al1 = 0.0, al2 = 0.0, tpk, xsp, ysp;

    //pi = 4. * atan(1.);
    pi = M_PI;

    FL2 << "\n" << finame << "            \n\n";

    FL7.open((GLOB.PROJECT_DIR + finame).c_str(), std::ios_base::binary | std::ios_base::out);

    scale(sc, sc);
    setpen(1);
    moveto(GLOB.rnorm * xz[0], GLOB.rnorm * yz[0]);
    t[0] = 0.;

    for (int i = 0; i < n + 1; ++i)
    {
        if (std::abs(rz[i]) < 1e-10)
        {
            xrz[i] = 0.;
            yrz[i] = 0.;
            alp[i] = 0.;
            alk[i] = 0.;
        }
        t[i] = 0.;
    }

    for (int i = 1; i < n + 1; ++i)
    {
        al1 = alp[i - 1];
        al2 = alk[i - 1];
        if (std::abs(rz[i - 1]) < 1e-10)
        {
            dx = xz[i] - xz[i - 1];
            dy = yz[i] - yz[i - 1];
            dl = std::sqrt(dx * dx + dy * dy);
            t[i] = t[i - 1] + dl;
            dt = (t[i] - t[i - 1]) / num;
            tb = t[i - 1];

            for (int j = 0; j < num; ++j)
            {
                tb += dt;
                xsp = xz[i - 1] + dx / dl * (tb - t[i - 1]);
                ysp = yz[i - 1] + dy / dl * (tb - t[i - 1]);
                drawto(GLOB.rnorm * xsp, GLOB.rnorm * ysp);
            }
        }
        else
        {
            tpk = std::abs(rz[i - 1] * (al2 - al1));
            t[i] = t[i - 1] + tpk;
            dt = tpk / num;
            tb = t[i - 1];

            for (int j = 0; j < num; ++j)
            {
                tb += dt;
                al = al1 + (tb - t[i - 1]) * (al2 - al1) / tpk;
                xsp = xrz[i - 1] + std::abs(rz[i - 1]) * cos(al);
                ysp = yrz[i - 1] + std::abs(rz[i - 1]) * sin(al);
                drawto(GLOB.rnorm * xsp, GLOB.rnorm * ysp);
            }
        }

        FL2 << std::setw(8) << std::setprecision(2) << GLOB.rnorm * xz[i - 1]
                << std::setw(8) << GLOB.rnorm * yz[i - 1] << std::setw(8) << GLOB.rnorm * rz[i - 1]
                << std::setw(8) << GLOB.rnorm * xrz[i - 1] << std::setw(8) << GLOB.rnorm * yrz[i - 1]
                << std::setw(8) << al1 * 180. / pi << std::setw(8) << al2 * 180. / pi
                << std::setw(8) << GLOB.rnorm * t[i - 1] << "\n";
    }

    FL7.close();

    FL2 << std::setw(8) << std::setprecision(2) << GLOB.rnorm * xz[n] << std::setw(8) << GLOB.rnorm * yz[n]
            << std::setw(8) << GLOB.rnorm * rz[n] << std::setw(8) << GLOB.rnorm * xrz[n]
            << std::setw(8) << GLOB.rnorm * yrz[n] << std::setw(8) << al1 * 180. / pi
            << std::setw(8) << al2 * 180. / pi << std::setw(8) << GLOB.rnorm * t[n] << "\n\n\n\n";

    return 0;
}

bool FilesManager::isMeshingNeeded()
{
    int nkszb = 0;

    std::ifstream FL14((GLOB.PROJECT_DIR + "last.msh").c_str(), std::fstream::binary);

    if (!FL14.is_open())
    {
        std::cout << "Nie mozna otworzyc pliku last.msh" << std::endl;
        return true;
    }
    else
    {
        FL14.seekg(4, std::ios::cur);
        FL14.read(reinterpret_cast<char*> (&nkszb), sizeof nkszb);
        FL14.seekg(4, std::ios::cur);

        if (nkszb != GLOB.nkp.nkszt)
        {
            std::cout << "Inny rodzaj szyny w pliku last.msh" << std::endl;
            FL14.close();
            return true;
        }
        
        // sprawdzic linie podzialu

    }
    FL14.close();
    return false;
}

int FilesManager::LoadMSH()
{

    int nkszb = 0;
    
    //  -------------------   obsluga pliku 'last.msh'    ----------------------

    std::ifstream FL14((GLOB.PROJECT_DIR + "last.msh").c_str(), std::fstream::binary);
    if (!FL14.is_open())
    {
        std::cerr << "Nie mozna otworzyc pliku last.msh" << std::endl;
        return -1;
    }
    else
    {
        FL14.seekg(4, std::ios::cur);
        FL14.read(reinterpret_cast<char*> (&nkszb), sizeof nkszb);
        FL14.seekg(4, std::ios::cur);

        for (int i = 0; i < 2 * GLOB.nkp.mw * GLOB.nkp.nw; ++i)
        {
            FL14.seekg(4, std::ios::cur);
            FL14.read(reinterpret_cast<char*> (&GLOB.zargd.xw[i]), sizeof GLOB.zargd.xw[i]);
            FL14.seekg(4, std::ios::cur);
        }
        FL14.close();
        return 1;
    }
}

int FilesManager::SaveMSH()
{
    // otworz do zapisu

    std::ofstream FL14;
    FL14.open((GLOB.PROJECT_DIR + "last.msh").c_str(), std::ios_base::binary | std::ios_base::out);

    FL14.write("4", 4);
    FL14.write(reinterpret_cast<char*> (&GLOB.nkp.nkszt), sizeof GLOB.nkp.nkszt);
    FL14.write("4", 4);

    for (int i = 0; i < 2 * GLOB.nkp.mw * GLOB.nkp.nw; ++i)
    {
        FL14.write("4", 4);
        FL14.write(reinterpret_cast<char*> (&GLOB.zargd.xw[i]), sizeof GLOB.zargd.xw[i]);
        FL14.write("4", 4);
    }

    FL14.close();
    return 0;
}
